"""
Simple test script to verify LM Studio API connection
and test with your actual comment format
"""

from openai import OpenAI
import json

# Connect to LM Studio
client = OpenAI(
    base_url="http://127.0.0.1:1234/v1",
    api_key="not-needed"
)

# Your actual test comments
test_comments = [
    "Cuando se repite mucho una palabra se llama MULETILLA, y todos las tenemos y si no la mayoría y no nos damos cuenta.",
    "Que se puede esperar de este Gobierno tan destructivo para México y se van a valer de más mañas y trampas"
]

print("Testing LM Studio API Connection")
print("="*60)

for i, comment in enumerate(test_comments, 1):
    print(f"\nTest {i}:")
    print(f"Comment: {comment[:80]}...")
    
    prompt = f"""Analyze this comment for violence content.

COMMENT:
{comment}

Respond ONLY with valid JSON:
{{
    "discusses_violence": true/false,
    "frequency_score": 0-10,
    "brief_explanation": "explanation"
}}"""

    try:
        response = client.chat.completions.create(
            model="qwen/qwen3-vl-4b",
            messages=[
                {"role": "system", "content": "You are a research assistant. Respond only with valid JSON."},
                {"role": "user", "content": prompt}
            ],
            temperature=0.2,
            max_tokens=200
        )
        
        result = response.choices[0].message.content
        print(f"Raw response: {result}")
        
        # Try to parse as JSON
        parsed = json.loads(result)
        print(f"✓ Successfully parsed JSON:")
        print(json.dumps(parsed, indent=2, ensure_ascii=False))
        
    except Exception as e:
        print(f"✗ Error: {e}")

print("\n" + "="*60)
print("If you see JSON responses above, the API is working correctly!")
print("Run violence_labeling.py to process your CSV files.")
